<?php
namespace Home\Widget;
use Think\Controller;
/**
 * 球场Widget
 */
class GymnasiumWidget extends Controller {
	//全局变量区
	/**
     * 布局相关
     * list_each_page_number：每一页列表条数
     */
    public $list_each_page_number = 5;
    
	/**
	 * 选定时间后展开球场预定界面
	 * $date：当前时间时间戳
	 * $gymnasium_id：当前球馆id
	 */
	public function courtLoad($timestamp, $gymnasium_id){
		//实例化球馆对象
		$gymnasium = M('gymnasium_table');
		//实例化停馆对象
		$gymnasiumClose = M('gymnasium_close_table');
		//实例化球场对象
		$court = M('court_table');
		//实例化球场预约对象
		$courtBook = M('court_book_table');

		//位置信息
		$e_page_num = $this->list_each_page_number;  //每一页的球馆条数
		$page_num = $_POST['page_num'];  //页数

		//置位
		$start_p = $e_page_num * ($page_num - 1);

		//组装查询条件
		$where['close_start_time'] = array('lt', $timestamp);
		$where['close_end_time'] = array('gt', $timestamp);
		$where['gymnasium_id'] = $gymnasium_id;
		//查询闭馆信息
		$result = $gymnasiumClose->where($where)->find();

		//如果不闭馆加载球场选择模板
		if($result){
			//当球馆停馆时不加载球场选择，在地下输出闭馆标识
			$this->assign('gymnasium_close_result', 1);
		}else{
			$this->assign('gymnasium_close_result', 0);

			//获取开闭关信息
			unset($where);
			$where['gymnasium_id'] = $gymnasium_id;
			$gymnasiumResult = $gymnasium->where($where)->find();
			$gymnasiumOpenTime = $gymnasiumResult['gymnasium_open_time'];
			$gymnasiumCloseTime = $gymnasiumResult['gymnasium_close_time'];
			$openHours = $gymnasiumCloseTime - $gymnasiumOpenTime;

			//整合球场信息
			$resultList =array();
			unset($where);
			$where['court_gymnasium_id'] = $result['gymnasium_id'];
			$courtResult = $court->where($where)->limit($start_p, $e_page_num)->select();
			$courtResultCount = $court->where($where)->count();
			//对每个球场进行加载
			for($courtN = 0; $courtN < $result['court_number']; $courtN++){
				$courtInfo = $courtResult[$courtN];
				$courtPassArray['court_name'] = $courtInfo['court_name'];//每个球场传往前端的信息
				unset($whereBook);
				$whereBook['court_id'] = $courtInfo['court_id'];
				$whereBook['order_date'] = $timestamp;
				$courtBookInfoList = $courtBook->where($whereCourt)->order('start_time ASC')->select();
				$courtBookNumber = $courtBook->where($whereCourt)->count();
				$bookInfoList = array();//对于每个球场每天预定情况数组，每个小时为一个项，0表示未被预定，1表示已被预定
				//数组初始化为0
				for($x1 = 0; $x1 < $openHours; $x1++){
					$bookInfoList[$x1] = 0;
				}
				for($bookN = 0; $bookN < $courtBookNumber; $bookN++){
					$courtBookInfo = $courtBookInfoList[$bookN];
					for($x1 = 0; $x1 < $courtBookInfo['last_units']; $x1++){
						$bookInfoList[$courtBookInfo['start_time'] - $gymnasiumOpenTime + $x1] = 1;
					}
				}
				$courtPassArray['book_info_list'] = $bookInfoList;
				$resultList[$courtN] = $courtPassArray;
			}
			$resultList['resultCount'] = $courtResultCount;
			$resultList['page_num'] = $page_num;
			$this->assign('resultList', $resultList);
		}
		$this->display('Gymnasium:courtLoad');
	}

	public function Gymnasium_list(){
		$this->display('Gymnasium:Gymnasium_list');
	}
}
